package floappcontest.core.backbeans;

import java.util.List;

import javax.faces.application.Application;
import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlInputText;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.component.html.HtmlPanelGrid;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;

import floappcontest.core.beans.Student;
import floappcontest.core.control.Control;
import floappcontest.core.dao.factory.HibernateDAOFactory;
import floappcontest.core.exception.FloAppException;

/**
 * The StudentBean is a backbean (Java bean) related with the 
 * student events (insert, delete, edit, etc) in the pages.  
 *
 */
public class StudentBean {

	private static final String FAILED = "FAILED"; //$NON-NLS-1$

	private static final String OK = "OK"; //$NON-NLS-1$

	private Control control;
	
	private Student student;
	
	private Student[] students;
	
	private List<Student> list;

	private Integer nos;
	
	
	public StudentBean() {
		Application app = FacesContext.getCurrentInstance().getApplication();
	    ValueBinding vb = FacesContext.getCurrentInstance().getApplication().createValueBinding( "#{teamBean.team.numberOfStudents}"  ); //$NON-NLS-1$
	    nos = (Integer)vb.getValue( FacesContext.getCurrentInstance() );
	    Student[] temp = getStudents();
		for ( int i = 0; i < getStudents().length; i++ ) {
			temp[i] = new Student();
		}
		setStudents( temp );
	    setControl( new Control( new HibernateDAOFactory() ) );
		setStudent( new Student() );
		refreshList();
	}
	
	public Student getStudent() {
		return this.student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}
	
	public String insertStudent() {
		try {
			System.out.println("INSERTINSERTINSERTINSERTINSERTINSERTINSERTINSERTINSERTINSERTINSERTINSERTINSERTINSERTINSERT");
			for ( Student s : getStudents() ) {
				getControl().insertStudent( s );
			}
			System.out.println("INSERTINSERTINSERTINSERTINSERTINSERTINSERTINSERTINSERTINSERTINSERTINSERTINSERTINSERTINSERT");
			nos = null;
			setStudent( new Student() );
			return OK;
		} catch (FloAppException e) {
			setStudent( new Student() );
			return FAILED;
		} finally{
			refreshList();
		}
	}
	
	/**
	 * @return
	 */
	public void refreshList(){		
		setList( getControl().listStudents() );		
	}
	
	public Control getControl() {
		return this.control;
	}

	public void setControl(Control control) {
		this.control = control;
	}

	public List<Student> getList() {
		return this.list;
	}

	public void setList(List<Student> list) {
		this.list = list;
	}
	
	public String removeStudent(){
		try {
			getControl().removeStudent( getStudent().getLogin() );
			return new MenuBarRedirection().listStudentPage();
		} catch (FloAppException e) {
			e.printStackTrace();
			return FAILED;
		}
	}

	
	public HtmlPanelGrid getFormElements() {
		Application app = FacesContext.getCurrentInstance().getApplication();
	    ValueBinding vb = FacesContext.getCurrentInstance().getApplication().createValueBinding( "#{teamBean.team.numberOfStudents}"  ); //$NON-NLS-1$
	    nos = (Integer)vb.getValue( FacesContext.getCurrentInstance() );
	    HtmlPanelGrid grid = new HtmlPanelGrid();
	    List<UIComponent> children = grid.getChildren();
	    for ( int i = 0; i < nos; i++ ) {
		    HtmlOutputText row = new HtmlOutputText();
		    row.setValue( i + 1 + ")    "  );
		    row.setStyleClass( "fieldLabel" );
		    children.add(row);

		    HtmlOutputText login1 = new HtmlOutputText();
		    login1.setValue("Login:");
		    login1.setId( "login1_" + i );
		    login1.setStyleClass( "fieldLabel" );
		    children.add(login1);
		    HtmlInputText login2 = new HtmlInputText();
		    login2.setRequired( true );
		    login2.setId( "login2_" + i );
		    login2.setValueBinding( "value", app.createValueBinding( "#{studentBean.students[" + i + "].login}" ) );
		    login2.setStyleClass( "fieldInput" );
		    children.add(login2);
		    
		    HtmlOutputText name1 = new HtmlOutputText();
		    name1.setValue("Nome:");
		    name1.setId( "name1_" + i );
		    name1.setStyleClass( "fieldLabel" );
		    children.add(name1);
		    HtmlInputText name2 = new HtmlInputText();
		    name2.setRequired( true );
		    name2.setId( "name2_" + i );
		    name2.setValueBinding( "value", app.createValueBinding( "#{studentBean.students[" + i + "].name}" ) );
		    name2.setStyleClass( "fieldInput" );
		    children.add(name2);
		    
		    HtmlOutputText email1 = new HtmlOutputText();
		    email1.setValue("email:");
		    email1.setId( "email1_" + i );
		    email1.setStyleClass( "fieldLabel" );
		    children.add(email1);
		    HtmlInputText email2 = new HtmlInputText();
		    email2.setRequired( true );
		    email2.setId( "email2_" + i );
		    email2.setValueBinding( "value", app.createValueBinding( "#{studentBean.students[" + i + "].email}" ) );
		    email2.setStyleClass( "fieldInput" );
		    children.add(email2);
		}
	    
	    return grid;
	}

	
	public Student[] getStudents() {
		return students;
	}

	
	public void setStudents( Student[] students ) {
		this.students = students;
	}

	
}
